/**
 * @file    damiao_drv.c
 * @brief   达妙电机相关接口
 * @details This is the detail description.
 * @author  wang-haoyu
 * @date    2025年7月10日 (created)
 * @version 0.0.1
 * @par Copyright (C):
 *          All Rights Reserved.
 * @par Encoding:
 *          UTF-8
 * @par Description        :
 * 1. Hardware Descriptions:
 *      None.
 * 2. Program Architecture:
 *      None.
 * 3. File Usage:
 *      None.
 * 4. Limitations:
 *      None.
 * 5. Else:
 *      None.
 * @par Modification:
 * -# Date          : 2025年7月10日;
 * Revision         : 0.0.1;
 * Author           : wang-haoyu;
 * Contents         :
 */
 
/* =============================================================================
 *                                header files
 * ========================================================================== */
#include "damiao_drv.h"
#include "jm_drv_master.h"

/* =============================================================================
 *                              macros & constants
 * ========================================================================== */

/* =============================================================================
 *                          data structure definitions
 * ========================================================================== */
static msg_tx_state_t msg_tx_state = jm_drv_tx_callback;
static msg_tx_urgent_state_t msg_tx_urgent_state = jm_drv_tx_urgent_callback;

/* =============================================================================
 *                         static variable definitions
 * ========================================================================== */

/* =============================================================================
 *                         static function definitions
 * ========================================================================== */

/* =============================================================================
 *                         global function definitions
 * ========================================================================== */
/*
 * 达秒电机错误码解析函数
 *
 * Parameter:
 *   Input:
 *		-state_word：状态字
 *   Output:
 * Returns:
 *	错误码
 */
rt_uint32_t damiao_drv_error_code_analys(rt_uint8_t state_word)
{
	if(state_word== 0x08)
		return err_joint_motor_over_voltage;
	else if(state_word == 0x09)
		return err_joint_motor_low_voltage;
	else if(state_word == 0x0A)
		return err_joint_motor_over_current;
	else if(state_word == 0X0B)
		return err_joint_motor_mos_over_tempreture;
	else if(state_word == 0X0C)
		return err_joint_motor_coil_over_tempreture;
	else if(state_word == 0X0D)
		return err_joint_motor_communication;
	else if(state_word == 0X0E)
		return err_joint_motor_overload;
	else
		return 0;
}

/*
* 达妙电机使/失能数据发送函数
 *
 * Parameter:
 *   Input:
 *		-drive_id：驱动器ID
 *		-sta_set：状态设置
 *		-CAN_frame_msg：数据包缓存区
 *   Output:
 * Returns:
 */
void damiao_drv_set_enable_state(rt_uint16_t node_id,bool sta_set)
{
    rt_err_t err;
    struct rt_can_msg msg;
	msg.id = node_id + JM_CTRL_MODE;
	msg.ide = RT_CAN_STDID;
	msg.rtr = RT_CAN_DTR;
	msg.len = FRAME_DATA_LEN;
	
	msg.data[0] = 0xff;
	msg.data[1] = 0xff;
	msg.data[2] = 0xff;
	msg.data[3] = 0xff;
	msg.data[4] = 0xff;
	msg.data[5] = 0xff;
	msg.data[6] = 0xff;
	if(sta_set == true)
		msg.data[7] = 0xfc;
	else
		msg.data[7] = 0xfd;

   msg_tx_state(&msg,sizeof(msg));
}

/*
* 达妙电机使/失能数据紧急发送函数
 *
 * Parameter:
 *   Input:
 *		-drive_id：驱动器ID
 *		-sta_set：状态设置
 *		-CAN_frame_msg：数据包缓存区
 *   Output:
 * Returns:
 */
void damiao_drv_set_enable_urgent_state(rt_uint16_t node_id,bool sta_set)
{
    rt_err_t err;
    struct rt_can_msg msg;
	msg.id = node_id + JM_CTRL_MODE;
	msg.ide = RT_CAN_STDID;
	msg.rtr = RT_CAN_DTR;
	msg.len = FRAME_DATA_LEN;
	
	msg.data[0] = 0xff;
	msg.data[1] = 0xff;
	msg.data[2] = 0xff;
	msg.data[3] = 0xff;
	msg.data[4] = 0xff;
	msg.data[5] = 0xff;
	msg.data[6] = 0xff;
	if(sta_set == true)
		msg.data[7] = 0xfc;
	else
		msg.data[7] = 0xfd;

   msg_tx_urgent_state(&msg,sizeof(msg));
}

/*
* 达妙电机零点保存数据发送函数
 *
 * Parameter:
 *   Input:
 *		-drive_id：驱动器ID
 *		-CAN_frame_msg：数据包缓存区
 *   Output:
 * Returns:
 */
void damiao_drv_save_zero_point_state(rt_uint16_t node_id)
{
    struct rt_can_msg msg;

	msg.id = node_id + JM_CTRL_MODE;
	msg.ide = RT_CAN_STDID;
	msg.rtr = RT_CAN_DTR;
	msg.len = FRAME_DATA_LEN;

	msg.data[0] = 0xff;
	msg.data[1] = 0xff;
	msg.data[2] = 0xff;
	msg.data[3] = 0xff;
	msg.data[4] = 0xff;
	msg.data[5] = 0xff;
	msg.data[6] = 0xff;
	msg.data[7] = 0xfe;
    msg_tx_state(&msg, sizeof(msg));
}

/*
* 达妙电机清错数据发送函数
 *
 * Parameter:
 *   Input:
 *		-drive_id：驱动器ID
 *		-CAN_frame_msg：数据包缓存区
 *   Output:
 * Returns:
 */
void damiao_drv_clear_error_state(rt_uint16_t node_id)
{
    rt_err_t err;
    struct rt_can_msg msg;

	msg.id = node_id + JM_CTRL_MODE;
	msg.ide = RT_CAN_STDID;
	msg.rtr = RT_CAN_DTR;
	msg.len = FRAME_DATA_LEN;
	
	msg.data[0] = 0xff;
	msg.data[1] = 0xff;
	msg.data[2] = 0xff;
	msg.data[3] = 0xff;
	msg.data[4] = 0xff;
	msg.data[5] = 0xff;
	msg.data[6] = 0xff;
	msg.data[7] = 0xfb;
    msg_tx_state(&msg, sizeof(msg));
}

/*
* 机械臂关节电机力位混控数据发送函数
 *
 * Parameter:
 *   Input:
 *		-node_id：驱动器ID
 *		-pos：期望位置
 *		-vel：最大速度限制
 *		-i_des：最大电流限制
 *		-obj：达秒电机设置结构体句柄
 *		-CAN_frame_msg：数据包缓存区
 *   Output:
 * Returns:
 */
void damiao_drv_force_pos_ctrl_state(rt_uint16_t node_id,float pos_des,rt_uint16_t vel_des,rt_uint16_t i_des)
{
	rt_uint8_t *pbuf;
	rt_uint16_t vel = 0,cur = 0;
    struct rt_can_msg msg;

	pbuf = (uint8_t*)&pos_des;
	vel = vel_des;
	cur = i_des;
	
	msg.id = node_id + JM_CTRL_MODE;
	msg.ide = RT_CAN_STDID;
	msg.rtr = RT_CAN_DTR;
	msg.len = FRAME_DATA_LEN;
	
	msg.data[0] = *pbuf;
	msg.data[1] = *(pbuf+1);
	msg.data[2] = *(pbuf+2);
	msg.data[3] = *(pbuf+3);
	
	msg.data[4] = vel & 0xff;
	msg.data[5] = (vel>>8) & 0xff;
	
	msg.data[6] = cur & 0xff;
	msg.data[7] = (cur>>8) & 0xff;
    msg_tx_state(&msg, sizeof(msg));
}

/*******************************************************************************
 *                                  EOF
 ******************************************************************************/
